package com.situ.stmall.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.situ.stmall.interceptor.ClearOrderInterceptor;
import com.situ.stmall.interceptor.UserInterceptor;

//相当于springmvc的配置文件

@Configuration
public class WebConfig implements WebMvcConfigurer{
	
	
	@Value("${spring.servlet.multipart.location}")
	private String path;
	
	@Autowired
	private UserInterceptor userInterceptor;
	
	@Autowired
	private ClearOrderInterceptor clearOrderInterceptor;
	
	//静态资源映射路径
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/upload/**")
			.addResourceLocations("file:"+path);
		
	}
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		//配置无业务逻辑的路径与视图的映射
		registry.addViewController("/").setViewName("index");
		registry.addViewController("/user/login").setViewName("user/login");
		registry.addViewController("/user/reg").setViewName("user/reg");
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(userInterceptor)
		.addPathPatterns("/user/**")//拦截路径
		.excludePathPatterns("/user/login","/user/reg");//放行路径
		
		
		registry.addInterceptor(clearOrderInterceptor)
		.addPathPatterns("/goods",
				"/user/cart/add",
				"/api/cart/**",
				"/user/order/add",
				"/api/order/getList");
	}
}
